/*   The MIT License
 *
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

%skeleton "./lalr1-ce-ext.cc"
%require "2.4"
%define parser_class_name "Parser"
%define namespace "Carina::CSP"

%code requires
{
#include "carina/common/global.hh"
#include "carina/common/logging.hh"
#include "carina/csp-driver.hh"
#include "carina/csp-ast.hh"
#include "carina/ast.hh"

#define YYSTYPE Carina::AST::UnmanagedNode
}

%name-prefix="csp_"
%parse-param { Driver& driver }
%lex-param { Driver& driver }

%locations
%initial-action
{
    @$.begin.filename = @$.end.filename = &driver.m_FileName;
};

//%debug
%error-verbose

%token                  T_END                   0   "end of file"

%token <Identifier>     T_IDENTIFIER                "identifier"
%token                  T_PROGRAM                   "program"
%token                  T_MATERIAL                  "material"
%token                  T_COMMON                    "common"
%token                  T_VARIABLE                  "variable"
%token                  T_TEXTURE                   "texture"
%token                  T_PROFILE                   "profile"
%token <StringLiteral>  T_STRING_LITERAL            "string literal"

%type <Parameter>                   parameter
%type <List>                        shader_program_body material_body profile_body
%type <Program>                     shader_program
%type <Material>                    material
%type <Profile>                     material_profile
%type <CommonVariables>             common_variables
%type <void>                        external_declaration translation_unit csp_file

//%printer { if($$) debug_stream() << $$->getValue(); } "identifier" "string literal"
//%printer { if($$) debug_stream() << $$->getName(); } "variable" "function" "type"

%destructor { $$.destroy(); } <*>

%start csp_file

%code provides
{
#define YY_DECL Carina::CSP::Parser::token_type csp_lex(YYSTYPE* yylval, \
                                                        Carina::CSP::Parser::location_type* yylloc, \
                                                        Carina::CSP::Driver& driver)
YY_DECL;

namespace Carina
{
namespace CSP
{
typedef AST::Value<string> Identifier;
}
}
}

%%

csp_file
    : translation_unit                                      { driver.setASTRoot($1); $$ = AST::Node(); }
    ;

translation_unit
    : /* empty */                                           { $$ = NodeT<AST::List>(); }
    | external_declaration translation_unit                 { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    ;

external_declaration
    : shader_program                                        { $$ = $1; }
    | material                                              { $$ = $1; }
    ;

shader_program
    : "program" "identifier" '{'
          shader_program_body
      '}'                                                   {
                                                                NodeT<Identifier> sp_body = $2;
                                                                CE_ASSERT(sp_body, "Expected shader program body");
                                                                $$ = CreateNode<Program>(@$, sp_body->getValue(), $4);
                                                            }
    ;

shader_program_body
    : /* empty */                                           { $$ = NodeT<AST::List>(); }
    | parameter shader_program_body                         { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    ;

parameter
    : "identifier" '=' "string literal" ';'                 {
                                                                NodeT<Identifier> param_name = $1;
                                                                NodeT<StringLiteral> param_value = $3;
                                                                CE_ASSERT(param_name && param_value, "Expected valid tokens");
                                                                $$ = CreateNode<Parameter>(@$, param_name->getValue(), param_value->getValue());
                                                            }
    ;

material
    : "material" "identifier" '{'
          material_body
      '}'                                                   {
                                                                NodeT<Identifier> material_name = $2;
                                                                CE_ASSERT(material_name, "Extected material description body");
                                                                $$ = CreateNode<Material>(@$, material_name->getValue(), $4);
                                                            }
    ;

material_body
    : /* empty */                                           { $$ = NodeT<AST::List>(); }
    | parameter material_body                               { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    | material_profile material_body                        { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    | common_variables material_body                        { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, $1, $2); }
    ;

material_profile
    : "profile" "identifier" '{'
         profile_body
      '}'                                                   {
                                                                NodeT<Identifier> profile_name = $2;
                                                                CE_ASSERT(profile_name, "Expected material description body");
                                                                $$ = CreateNode<Profile>(@$, profile_name->getValue(), $4);
                                                            }
    ;

common_variables
    : "common" '{'
         profile_body
      '}'                                                   { $$ = CreateNode<CommonVariables>(@$, $3); }
    ;

profile_body
    : /* empty */                                           { $$ = NodeT<AST::List>(); }
    | "variable" "identifier" ';' profile_body              { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, CreateNodeTyped<Variable>(@$, $2->getValue()), $4); }
    | "texture" "identifier" ';' profile_body               { $$ = CreateNode<AST::List>(@$, CE_AST_SEMICOLON_SEPARATED_LIST, CreateNodeTyped<Texture>(@$, $2->getValue()), $4); }
    ;
%%

namespace Carina
{
namespace CSP
{
void Parser::error(const Parser::location_type& l, const std::string& m)
{
    driver.error(l, m);
}
}
}
